// 创建一个路由器，并暴露出去
 
// 引入createRouter
import {createRouter,createWebHistory} from 'vue-router'
 
//引入可能会用到的组件
// import LoginPage from '@/components/LoginPage.vue'
// import MainPage from '@/components/MainPage.vue'
// import MyPage from '@/components/MyPage.vue'
// import RegisterPage from '@/components/RegisterPage.vue'
// import HelloWorld from '@/components/HelloWorld.vue'
// import WelcomeItem from '@/components/WelcomeItem.vue'
// import index from '@/layout/index.vue'
//import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
import { staticRoutes } from './staticRoutes';
import defaultRoutes from './defaultRoutes';


/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
//路由表

const routes: any = staticRoutes.concat(defaultRoutes);
// export const routes = [
//     {path:'/LoginPage', component: LoginPage},
//     {path:'/MainPage', component: MainPage},
//     {path:'/MyPage', component: MyPage},
//     {path:'/RegisterPage', component: RegisterPage},
//     {path:'/HelloWorld', component: HelloWorld},
//     {path:'/WelcomeItem', component: WelcomeItem},
//     {path:'/index', component: index}
// ]

//路由器
const router = createRouter({
    history: createWebHistory(),
    routes
})


router.beforeEach((to, from, next) => {
    let userInfo = localStorage.getItem('user');
    if (to.path === "/login") {
      next();
    } else {
      if (userInfo) {
        next();
      } else {
        next({
          path: '/login'
        });
      }
    }
  });

//暴露出去router
export default router